﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace BHA
{
    public class PerfType
    {
        public const string PERFTYPE_REGEX = @"PERFTYPE.\d+";

        #region STATIC
        public static bool IsPerfType(string Input)
        {
            if (string.IsNullOrWhiteSpace(Input)) return false;
            return Regex.IsMatch(Input, PERFTYPE_REGEX);
        }
        #endregion

        //public List<ItemSAP> Items { get; set; }
        public List<ItemSAP> Items { get; set; }
        public string CODE { get; set; }
        public string Description { get; set; }
        public string WellName { get; set; }
        public string ZoneName { get; set; }
        public string TopPerf { get; set; }
        public string BotPerf { get; set; }
        public string Interval { get; set; }
        public string Temp { get; set; }
        public string Pressure { get; set; }
        public string EDA { get; set; }
        public string GunPerRun { get; set; }
        public string Backup { get; set; }
        public PerfType()
        {
            this.CODE = "Unknown";
            this.Description = "Unknown";
            this.WellName = "Unknown";
            this.ZoneName = "Unknown";
            this.TopPerf = "Unknown";
            this.BotPerf = "Unknown";
            this.Interval = "Unknown";
            this.Temp = "Unknown";
            this.Pressure = "Unknown";
            this.EDA = "Unknown";
            this.GunPerRun = "Unknown";
            this.Backup = "Unknown";
            this.Items = new List<ItemSAP>();
        }

        /// <summary>
        /// Clone PerfType from DB with VarCode to got exact Quantity of each ItemSAP. Eg : "{Interval}100,{EDA}3,"
        /// </summary>
        /// <param name="VarCode"></param>
        /// <returns></returns>
        public PerfType Clone(string VarCode = null)
        {
            // Clone all Property but not Items (Sub Item)
            PerfType perftype = new PerfType();
            perftype.CODE = this.CODE;
            perftype.Description = this.Description;
            perftype.WellName = this.WellName;
            perftype.ZoneName = this.ZoneName;
            perftype.TopPerf = this.TopPerf;
            perftype.BotPerf = this.BotPerf;
            perftype.Interval = this.Interval;
            perftype.Temp = this.Temp;
            perftype.Pressure = this.Pressure;
            perftype.EDA = this.EDA;
            perftype.GunPerRun = this.GunPerRun;
            perftype.Backup = this.Backup;

            if (VarCode != null & this.Items.Count > 0)
            {
                perftype.Items = new List<ItemSAP>();
                foreach (ItemSAP item in this.Items)
                {
                    ItemSAP clone = item.CloneQuanFormula(VarCode);
                    perftype.Items.Add(clone);
                }
            }
            else perftype.Items = this.Items;

            return perftype;
        }

        public List<ItemSAP> ExtendList(List<ItemSAP> database = null)
        {
            if (database == null)
            {
                List<ItemSAP> list = new List<ItemSAP>();
                foreach (ItemSAP item in this.Items)
                {
                    bool found = false;
                    foreach (ItemSAP previousitem in list)
                    {   // found if duplicate
                        if (previousitem.NoSAP == item.NoSAP)
                        {
                            previousitem.Quantity += item.Quantity;
                            found = true;
                        }
                    }
                    if (!found) // If not found in list (not duplicate)
                    {
                        list.Add(item);
                    }
                }
                return list;
            }
            else
            {
                List<ItemSAP> list = new List<ItemSAP>();
                foreach (ItemSAP item in this.Items)
                {   // Save SAP Item from DB To List
                    bool found = false;
                    foreach (ItemSAP itemdb in Globals.DBEquip.DatabaseEquip)
                    {
                        if (itemdb.NoSAP == item.NoSAP) { list.Add(itemdb.Clone(item.Quantity)); found = true; }
                    }
                    if (!found) // If not found in DB then save current to List
                    {
                        list.Add(item);
                    }
                }

                List<ItemSAP> listext = new List<ItemSAP>();
                foreach (ItemSAP item in list)
                {   // Duyet qua tung item trong list
                    if (item.Items.Count == 0)
                    {   // Neu khong co Sub Item thi add truc tiep
                        bool found = false;
                        foreach (ItemSAP eitem in listext)
                        {   // Kiem tra neu trung thi tang so luong
                            if (eitem.NoSAP == item.NoSAP) { eitem.Quantity += item.Quantity; found = true; }
                        }
                        if (!found) listext.Add(item);
                    }
                    else
                    {   // Neu co Sub Item thi add Sub Item Only
                        foreach (ItemSAP subitem in item.Items)
                        {
                            bool found = false;
                            foreach (ItemSAP eitem in listext)
                            {
                                if (eitem.NoSAP == subitem.NoSAP) { eitem.Quantity += item.Quantity * subitem.Quantity; found = true; }
                            }
                            if (!found) listext.Add(subitem.Clone(item.Quantity * subitem.Quantity));
                        }
                    }
                }

                return listext;
            }
        }

        double QuantityFromCode(string code)
        {
            return 0;
        }
    }
}
